package junweixu.jreader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class ReadActivity extends Activity
{
    class loadBook extends AsyncTask<String, Void, String>
    {

        @Override
        protected String doInBackground(String... params)
        {
            // TODO Auto-generated method stub
            File mFile = new File(params[0]);
            FileInputStream fileInputStream = null;
            try
            {
                fileInputStream = new FileInputStream(mFile);
            } catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            InputStreamReader inputStreamReader = null;
            try
            {
                inputStreamReader = new InputStreamReader(fileInputStream,
                        "gbk");
            } catch (UnsupportedEncodingException e1)
            {
                e1.printStackTrace();
            }

            BufferedReader reader = new BufferedReader(inputStreamReader);
            StringBuffer sb = new StringBuffer("");
            String line;
            try
            {
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line);
                    sb.append("\n");
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            return sb.toString();
        }

        @Override
        protected void onPostExecute(String result)
        {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            readingTextView.setVisibility(View.GONE);
            bookContenTextView.setText(result);
            bookContenTextView.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPreExecute()
        {
            // TODO Auto-generated method stub
            readingTextView.setVisibility(View.VISIBLE);
            bookContenTextView.setVisibility(View.GONE);
            super.onPreExecute();
        }

    }

    private TextView bookContenTextView;
    private TextView readingTextView;
    private static final int TEXT_COLOR_ITEM_1 = Menu.FIRST + 100;
    private static final int TEXT_COLOR_ITEM_2 = Menu.FIRST + 101;
    private static final int TEXT_COLOR_ITEM_3 = Menu.FIRST + 102;
    private static final int TEXT_COLOR_ITEM_4 = Menu.FIRST + 103;
    private static final int TEXT_COLOR_ITEM_5 = Menu.FIRST + 104;
    private static final int BG_COLOR_ITEM_1 = Menu.FIRST + 205;
    private static final int BG_COLOR_ITEM_2 = Menu.FIRST + 206;
    private static final int BG_COLOR_ITEM_3 = Menu.FIRST + 208;
    private static final int BG_COLOR_ITEM_4 = Menu.FIRST + 209;
    private static final int BG_COLOR_ITEM_5 = Menu.FIRST + 207;
    private static final int TEXT_SIZE_ITEM_1 = Menu.FIRST + 301;
    private static final int TEXT_SIZE_ITEM_2 = Menu.FIRST + 302;
    private static final int TEXT_SIZE_ITEM_3 = Menu.FIRST + 303;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.read_activity);
        bookContenTextView = (TextView) findViewById(R.id.bookContentTextView);
        readingTextView = (TextView) findViewById(R.id.readingTextView);
        bookContenTextView.setMovementMethod(ScrollingMovementMethod
                .getInstance());
        Intent intent = getIntent();
        String mPath = intent.getStringExtra("path");
        new loadBook().execute(mPath);
        Log.e("shit", mPath);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // TODO Auto-generated method stub
        SubMenu textColor = menu.addSubMenu("字体颜色");
        SubMenu bgColor = menu.addSubMenu("背景颜色");
        SubMenu textSize = menu.addSubMenu("字体大小");
        textColor.add(0, TEXT_COLOR_ITEM_1, 0, "黑");
        textColor.add(0, TEXT_COLOR_ITEM_2, 0, "白");
        textColor.add(0, TEXT_COLOR_ITEM_3, 0, "红");
        textColor.add(0, TEXT_COLOR_ITEM_4, 0, "绿");
        textColor.add(0, TEXT_COLOR_ITEM_5, 0, "蓝");
        bgColor.add(0, BG_COLOR_ITEM_1, 0, "黑");
        bgColor.add(0, BG_COLOR_ITEM_2, 0, "白");
        bgColor.add(0, BG_COLOR_ITEM_3, 0, "红");
        bgColor.add(0, BG_COLOR_ITEM_4, 0, "绿");
        bgColor.add(0, BG_COLOR_ITEM_5, 0, "蓝");
        textSize.add(0, TEXT_SIZE_ITEM_1, 0, "12");
        textSize.add(0, TEXT_SIZE_ITEM_2, 0, "20");
        textSize.add(0, TEXT_SIZE_ITEM_3, 0, "25");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // TODO Auto-generated method stub

        switch (item.getItemId())
        {
        case TEXT_COLOR_ITEM_1:
            bookContenTextView.setTextColor(Color.BLACK);
            break;
        case TEXT_COLOR_ITEM_2:
            bookContenTextView.setTextColor(Color.WHITE);
            break;
        case TEXT_COLOR_ITEM_3:
            bookContenTextView.setTextColor(Color.RED);
            break;
        case TEXT_COLOR_ITEM_4:
            bookContenTextView.setTextColor(Color.GREEN);
            break;
        case TEXT_COLOR_ITEM_5:
            bookContenTextView.setTextColor(Color.BLUE);
            break;
        case BG_COLOR_ITEM_1:
            bookContenTextView.setBackgroundColor(Color.BLACK);
            break;
        case BG_COLOR_ITEM_2:
            bookContenTextView.setBackgroundColor(Color.WHITE);
            break;
        case BG_COLOR_ITEM_3:
            bookContenTextView.setBackgroundColor(Color.RED);
            break;
        case BG_COLOR_ITEM_4:
            bookContenTextView.setBackgroundColor(Color.GREEN);
            break;
        case BG_COLOR_ITEM_5:
            bookContenTextView.setBackgroundColor(Color.BLUE);
            break;
        case TEXT_SIZE_ITEM_1:
            bookContenTextView.setTextSize(12);
            break;
        case TEXT_SIZE_ITEM_2:
            bookContenTextView.setTextSize(20);
            break;
        case TEXT_SIZE_ITEM_3:
            bookContenTextView.setTextSize(25);
            break;
        }

        return super.onOptionsItemSelected(item);
    }
}
